home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / ncsat.cpt / Telnet2.5 final / tcpip / slip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-05  |  3.7 KB  |  192 lines

  1. /*
  2. *   SLIP routines
  3. *
  4. *   new with NCSA/BYU Telnet version 2.4.15
  5. *
  6. *   by Jim Logan Feb 1992
  7. *
  8. */
  9.  
  10. #include <Dialogs.h>
  11. #include <memory.h>
  12. #include <OSUtils.h>
  13. #include <string.h>
  14.  
  15. #include "configrec.h"
  16. #include "protocol.h"
  17. #include "ip.h"
  18. #include "data.h"
  19. #include "wind.h"
  20.  
  21. #include "maclook.h"
  22. #include "menu.h"
  23. #include "netevent.h"
  24. #include "ser.h"
  25.  
  26. #define input_size 8192
  27. #define input_buf_size 12288
  28. #define output_buf_size 8192
  29. #define SLIP_IN_BUFS 64
  30.  
  31. #define SLIP_END 0xc0
  32. #define SLIP_ESC 0xdb
  33. #define SLIP_ESC_END 0334
  34. #define SLIP_ESC_ESC 0335
  35.  
  36. static char slip_end = 0xc0;
  37. static char slip_esc_end[] = { 0xdb,0xdc };
  38. static char slip_esc_esc[] = { 0xdb,0xdd };
  39.  
  40. short
  41.     old_received = 0,
  42.     received = 0,
  43.     slipfirstbuf = 0,
  44.     sliplastbuf = 0;
  45.  
  46. extern WindRec *screens;        /* From "maclook.c" */
  47.  
  48. extern short slip_connection;
  49.  
  50. extern unsigned char SLIP_ip_number[];
  51.  
  52. unsigned char *slipinptr[SLIP_IN_BUFS],*slipinbuf,*slipoutbuf;
  53.  
  54.  
  55. initslip() {
  56. long mysize;
  57.     mysize = input_buf_size;
  58.     slipinbuf = (unsigned char *) NewPtr(mysize);
  59.     mysize = output_buf_size;
  60.     slipoutbuf = (unsigned char *) NewPtr(mysize);
  61.     slipinptr[0] = slipinbuf;
  62. }
  63.  
  64.  
  65. switchtoslip(pnum) int pnum; {
  66.     struct port *p;
  67.     int slipscrn;
  68.  
  69.     if (pnum < 0 || pnum >= NPORTS)
  70.         return;
  71.  
  72.     if (NULL == (p = portlist[pnum]))
  73.         return;
  74.  
  75.     p->state = SCLOSED;
  76.     p->in.port = p->out.port = 0;
  77.  
  78.     slip_connection = 1;
  79.  
  80. /* Close the serial connection, and require the operator 
  81.    to open SLIP sessions with the "Open" function 
  82.    under the "File" menu. */
  83.  
  84.     slipscrn = WindByPort(pnum);
  85.     if (slipscrn >= 0) 
  86.       destroyport(slipscrn);
  87.  
  88.     setupport(0);                /* Open a connection, maybe even SLIP */                
  89. }
  90.  
  91.  
  92. int SLIPdlayersend(unsigned char *ptr,int size) {
  93.     int pos;
  94.  
  95.     ptr += sizeof(DLAYER);
  96.     size -= sizeof(DLAYER);
  97.     pos = 1;
  98.     slipoutbuf[0] = slip_end;
  99.     while (size > 0) {
  100.         switch (*ptr) {
  101.             case SLIP_END:
  102.                 slipoutbuf[pos++] = slip_esc_end[0];
  103.                 slipoutbuf[pos++] = slip_esc_end[1];
  104.                 break;
  105.             case SLIP_ESC:
  106.                 slipoutbuf[pos++] = slip_esc_esc[0];
  107.                 slipoutbuf[pos++] = slip_esc_esc[1];
  108.                 break;
  109.             default:
  110.                 slipoutbuf[pos++] = *ptr;
  111.         }
  112.         ptr++;
  113.         size--;
  114.     }
  115.     slipoutbuf[pos++] = slip_end;
  116.     write_serial(slipoutbuf,pos);
  117.     return(size);
  118. }
  119.  
  120.  
  121. SLIPreceive(unsigned char *ptr,int size) {
  122.     while (size > 0) {
  123.         switch (*ptr) {
  124.             case SLIP_END:
  125.  
  126.                 if (received != old_received) {
  127.  
  128. /* Don't allow the IP header to begin on         /* BYU 2.4.19 */
  129. /* an odd byte or MacPlus will crash.            /* BYU 2.4.19 */
  130.                     if (received > input_size)
  131.                         received = 0;
  132.                     else if (received & 1)        /* BYU 2.4.19 */
  133.                         received++;                /* BYU 2.4.19 */
  134.  
  135.                     sliplastbuf++;
  136.                     if (sliplastbuf >= SLIP_IN_BUFS)
  137.                         sliplastbuf = 0;
  138.  
  139.                     /* Prepare the next buffer space */
  140.                     slipinptr[sliplastbuf] = slipinbuf + received;
  141.  
  142.                     if ((slipinbuf + received) == slipinptr[slipfirstbuf])
  143.                         SysBeep(4);                            /* Buffer overflow/overrun */
  144.  
  145.                     old_received = received;
  146.                 }
  147.                 break;
  148.             case SLIP_ESC:
  149.                 size--;
  150.                 if (size <= 0)
  151.                     return;
  152.                 ptr++;
  153.                 switch (*ptr) {
  154.                     case SLIP_ESC_END:
  155.                         if (received < input_size)
  156.                             slipinbuf[received++] = SLIP_END;
  157.                         break;
  158.                     case SLIP_ESC_ESC:
  159.                         if (received < input_size)
  160.                             slipinbuf[received++] = SLIP_ESC;
  161.                         break;
  162.                     default:
  163.                         slipinbuf[received++] = *ptr;        /* Hmmmm, questionable! */
  164.                         break;
  165.                 }
  166.                 break;
  167.             default:
  168.                 if (received < input_buf_size)
  169.                     slipinbuf[received++] = *ptr;
  170.         }
  171.         size--;
  172.         ptr++;
  173.     }
  174. }
  175.  
  176.  
  177. int SLIPdemux(int all) {
  178. #pragma unused(all)
  179.     int nmuxed;
  180.  
  181.     nmuxed = 0;
  182.     while (slipfirstbuf != sliplastbuf) {
  183.         ipinterpret((IPKT *)(slipinptr[slipfirstbuf] - sizeof(DLAYER)),FROM_SLIP);
  184.         slipfirstbuf++;
  185.         if (slipfirstbuf >= SLIP_IN_BUFS)
  186.             slipfirstbuf = 0;
  187.         nmuxed++;
  188.     }
  189.  
  190.     return(nmuxed);
  191. }
  192.